#include "SuperTurret.h"

#define BULLET_VECTOR_0 5
#define PI 3.14159265

int CSuperTurret::_GunXPosition[3] = {-32, -32, -16};
int CSuperTurret::_GunYPosition[3] = {0, 16, 32};
double CSuperTurret::_ShootAngle[3] = {180, 150, 120};

CSuperTurret::CSuperTurret(int X, int Y)
{
	_Sprite = CSpriteManager::getInstance()->GetEnemySprite(Enemy::SuperTurret);
	_x = X;
	_y = Y;
	_count = 0;
	frame_start = GetTickCount();
	_ShootCount = 0;
	tick_per_frame = 1000 / 5;
	_CurrentFrame = 0;
	_ListBullet = new CListBullet(6);
}

CSuperTurret::~CSuperTurret(void)
{
}

void CSuperTurret::Update(D3DXVECTOR2 ContraPosition)
{
	
	int angle = GetAngle(ContraPosition, this->GetPosition());
	if(_count < 1)
	{
		_IsShootable = false;
		_CurrentFrame++;
		_count++;
	}
	else
	{
		if(_count == 1)
		{
			shoot_chain_start = GetTickCount() - 1500;
			_count++;
		}
		if(angle >= 165 && angle < 345)
		{
			_CurrentFrame = 2;
			return;
		}
		if(angle >= 135 && angle < 165)
		{
			_CurrentFrame = 3;
			return;
		}
		_CurrentFrame = 4;
	}
}

void CSuperTurret::Render(D3DXVECTOR2 ContraPosition)
{
	if ((ContraPosition.x < SCREEN_WIDTH / 2 && abs(ContraPosition.x - _x) <= SCREEN_WIDTH + FRAMEWIDTH) || abs(ContraPosition.x - _x) < SCREEN_WIDTH / 2 || (ContraPosition.x > GAMEWORLDWIDTH - SCREEN_WIDTH / 2 && abs(_x - ContraPosition.x) < SCREEN_WIDTH + FRAMEWIDTH))
	{
		DWORD now = GetTickCount();	
		if(now - shoot_chain_start >= 2000)
		{
			_IsShootable = true;
			shoot_chain_start = now;
			_ShootCount = 0;
			shoot_start = now - 200;
		}
		if (now - frame_start >= tick_per_frame)
		{
			Update(ContraPosition);
			frame_start = now;
		}
		if(_IsShootable)
		{
			if(now - shoot_start >= 200)
			{
				_ListBullet->Shoot(_x + _GunXPosition[_CurrentFrame - 2], _y + _GunYPosition[_CurrentFrame - 2], 3000, BULLET_VECTOR_0 * cos(_ShootAngle[_CurrentFrame - 2]/180*PI), BULLET_VECTOR_0 * sin(_ShootAngle[_CurrentFrame - 2]/180*PI), 2);
				_ShootCount++;
				shoot_start = now;
			}
			if(_ShootCount == 3)
			{
				_IsShootable = false;
			}
		}
		_ListBullet->Render();

		_Sprite->Render(_x, _y, _CurrentFrame);
	}
}

Box CSuperTurret::GetBox()
{
	return Box(_x - FRAMEWIDTH/2, _y+FRAMEHEIGHT/2, FRAMEWIDTH, FRAMEHEIGHT, 0.0f, 0.0f);
}